﻿using AntdUI;
using Super.Admin.ViewModels.Base;
using System.Collections.ObjectModel;

namespace Super.Admin.Models.Permission
{
    public class PermissionBaseModel : ViewModelBase
    {
        /// <summary>
        /// 权限ID
        /// </summary>
        public int Id { get; set; } = -1;
        private string _name;
        /// <summary>
        /// 权限名称
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                SetProperty(ref _name, value);
            }
        }
        /// <summary>
        /// 权限类型
        /// </summary>
        public int Type { get; set; }
        /// <summary>
        /// Type 标签
        /// </summary>
        public CellTag[] TypeTag { get; set; }
        /// <summary>
        /// 父级权限ID
        /// </summary>
        public int ParentId { get; set; }
        /// <summary>
        /// 权限标识
        /// </summary>
        public string Permissionkey { get; set; }
        /// <summary>
        /// 权限路由
        /// </summary>
        public string Path { get; set; }
        /// <summary>
        /// Icon文本
        /// </summary>
        public CellImage IconText { get; set; }
        /// <summary>
        /// 权限图标
        /// </summary>
        public string Icon { get; set; }
        /// <summary>
        /// 权限排序
        /// </summary>
        public int Sort { get; set; }
        /// <summary>
        /// 是否激活
        /// </summary>
        public int IsActive { get; set; }
        /// <summary>
        /// 是否激活Tag
        /// </summary>
        public CellTag[] IsActiveTag { get; set; }
        private int _level;
        /// <summary>
        /// 缩进层级
        /// </summary>
        public int Level
        {
            get => _level;
            set { SetProperty(ref _level, value); }
        }
        public bool IsCanClose { get; set; } = true;
        private bool _isExpanded;
        /// <summary>
        /// 是否展开
        /// </summary>
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set { SetProperty(ref _isExpanded, value); }
        }
        private PermissionBaseModel _permissionParent;
        /// <summary>
        /// 父级权限模型
        /// </summary>
        public PermissionBaseModel PermissionParent
        {
            get { return _permissionParent; }
            set { SetProperty(ref _permissionParent, value); }
        }
        private bool? _isChecked = false;
        /// <summary>
        /// 用于角色管理 是否选中
        /// </summary>
        public bool? IsChecked
        {
            get { return _isChecked; }
            set
            {
                // 如果值未改变则直接返回
                if (SetProperty(ref _isChecked, value))
                {
                    if (HasChild && value.HasValue)
                    {
                        SetChildIsChecked(this, value);
                    }
                    else if (value == null)
                    {
                        if (!HasChild || !CheckChildIsFullCheck(this))
                        {
                            value = true;
                            SetProperty(ref _isChecked, value);
                        }
                    }
                    if (PermissionParent != null) SetParentIsChecked(this, value);
                }
            }
        }
        /// <summary>
        /// 检查子权限是否全部选中或未选中
        /// </summary>
        /// <param name="permissionBaseModel"></param>
        /// <returns></returns>
        private bool CheckChildIsFullCheck(PermissionBaseModel permissionBaseModel)
        {
            return permissionBaseModel.PermissionChildren.Where(p => p.IsChecked == false || p.IsChecked == null).Count() > 0;
        }
        /// <summary>
        /// 设置父级的选中状态
        /// </summary>
        /// <param name="permission"></param>
        /// <param name="value"></param>
        private void SetParentIsChecked(PermissionBaseModel permission, bool? value)
        {
            if (value == null)
            {
                permission.PermissionParent.IsChecked = null;
            }
            else if (value == true)
            {
                if (permission.PermissionParent.PermissionChildren.Where(p => p.IsChecked == false || p.IsChecked == null).Count() > 0)
                {
                    permission.PermissionParent.IsChecked = null;
                }
                else
                {
                    permission.PermissionParent.IsChecked = true;
                }
            }
            else
            {
                if (permission.PermissionParent.PermissionChildren.Where(p => p.IsChecked == true || p.IsChecked == null).Count() > 0)
                {
                    permission.PermissionParent.IsChecked = null;
                }
                else
                {
                    permission.PermissionParent.IsChecked = false;
                }
            }
        }
        public PermissionBaseModel[] Children { get; set; }
        /// <summary>
        /// 子权限集合
        /// </summary>
        public List<PermissionBaseModel> PermissionChildren { get; set; }
        /// <summary>
        /// 是否有子权限
        /// </summary>
        public bool HasChild { get { return PermissionChildren != null && PermissionChildren.Count > 0; } }

        /// <summary>
        /// 操作按钮
        /// </summary>
        public CellLink[] Operates { get; set; }

        /// <summary>
        /// 设置子的选中状态
        /// </summary>
        /// <param name="permission"></param>
        /// <param name="isChecked"></param>
        private void SetChildIsChecked(PermissionBaseModel permission, bool? isChecked)
        {
            foreach (var child in permission.PermissionChildren)
            {
                child._isChecked = isChecked;
                child.OnPropertyChanged(nameof(IsChecked));

                if (child.HasChild)
                {
                    SetChildIsChecked(child, isChecked);
                }
            }
        }
    }
}
